﻿
$(function () {
    $('.banner-swipe').children('div').height($('body').width() * 280 / 640);

    //banner
    var $swipe = $('.banner-swipe'),
        $swipeWrap = $swipe.children('div'),
        $swipePosition = $('.banner-position'),
        length = $swipeWrap.children('a').length;
    if (length > 1) {
        var swipePosition = '';
        $swipeWrap.children('a').wrap('<div></div>');
        for (i = 0; i < length; i++) {
            if (i == 0) {
                swipePosition += '<li class="cur"></li>';
            } else {
                swipePosition += '<li></li>';
            }
        }
        $swipePosition.append(swipePosition);
        var bullets = $swipePosition.children('li');
        window.bannerSlider = Swipe(document.getElementsByClassName('banner-swipe').item(0), {
            auto: 3000,
            startSlide: 0,
            continuous: true,
            disableScroll: false,
            callback: function (index) {
                if ((index + 1) > bullets.length) {
                    index = index % 2;
                }
                bullets.attr('class', '').eq(index).attr('class', 'cur');
            }
        });
    }
    else {
        $swipe.css('visibility', 'visible');
    }
    $(".items img").lazyload({ threshold: 500 });
    //icon宽度自适应
    if ($('.icons a').length < 4) {
        $('.icons a').css('width', (100 / $('.icons a').length) + '%')
    }
    //item图片自适应高度
    var imgWidth = ($('.items').width() - 41) / 2;
    $('.items img').css('height', imgWidth * 140 / 225)
});

(function () {
    $(function () {
        if (navigator.userAgent.match(/(iphone|ipad|Android|ios)/ig)) {
            //app状态监控，用户10秒不触碰屏幕，即有可能离开了app，停止banner
            var inApp = true;
            function inAppTimerFn() {
                window.inAppTimer = setTimeout(function () {
                    inApp = false;
                    if (window.bannerSlider) {
                        bannerSlider.stop();
                    }
                }, 12000);
            }
            inAppTimerFn();
            document.addEventListener('touchend', function (e) {
                if (window.inAppTimer) clearTimeout(window.inAppTimer);
                if (!inApp) {
                    inApp = true;
                    if (window.bannerSlider) {
                        bannerSlider.begin();
                    }
                }
                inAppTimerFn();
            }, false)
        }
    })
})()
function onDBNewOpenBack() {
    refresh();
}
//刷新首页用户积分
function refresh() {
    $.ajax({
        type: "post", url: '/ctool/getCredits', dataType: "json",
        success: function (data) {
            //当积分变动的时候，调取客户端方法，回调积分。
            try {
                if ($("#db-credits-num").text() != data.credits && window.duiba_app && window.duiba_app.localRefresh) {
                    duiba_app.localRefresh(data.credits);
                }
            } catch (e) {
            }
            //商城首页更新积分
            $("#db-credits-num").text(data.credits);
            //更新是否有新的兑换记录
            if (data.status) {
                $('#db-new-record').show();
            }
            else {
                $('#db-new-record').hide();
            }
        }
    });
}
(function () {
    //开始进入页面获取是否有新记录
    var hasNewRecord = false;
    if (hasNewRecord) {
        $('#db-new-record').show();
    }
    else {
        $('#db-new-record').hide();
    }

    Origami.fastclick(document.body);
})();
